(function(){

    $(document).on('ready', function() {
        $( "#accordion" ).accordion({
            heightStyle: "content"
        });

        if (!isAuthorized()) {
            var popup = new Popup('Помилка', '<div>Ви не авторизовані в системі. У Вас є можлівисть <a href="#" id="sug-login" class="italic">авторизуватись</a> або <a href="#" id="sug-register" class="italic">зареєструватись</a></div>', 'white', function(popupId){
                var $popupEl = $(popupId);
                $popupEl.find('#sug-login').on('click', function(event) {
                    popup.destroy();
                    login(function(){window.location.reload();});
                });
                $popupEl.find('#sug-register').on('click', function(event) {
                    popup.destroy();
                    register(function(){window.location.reload();});
                });
            });
            popup.show();
            return;
        }

        new Request('allTickets').send(function(data){
            var tickets = data['tickets'];
            var $table = $('.table-tickets tbody');
            for (var i = 0, size = tickets.length; i < size; i++) {
                var ticket = tickets[i];
                $table.append(uc.ticketRecordTemplate(ticket['passenger'], ticket['phones'], ticket['trip'], ticket['startDate'], ticket['seat'], ticket['status']));
            }
        });

    });

    window.uc.ticketRecordTemplate = function(passenger, phones, trip, startDate, seat, status) {
        return '<tr><td>'+passenger+'</td><td>'+phones+'</td><td>'+trip+'</td><td>'+startDate+'</td><td>'+seat+'</td><td>'+status+'</td></tr>';
    }

})();
